package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.CStructureCiEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.CsValueEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.MsCoefficientEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.RcStructureRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class RcStructureRepositoryImpl implements RcStructureRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saverCStructureCi(CStructureCiEntity it) {
        jdbcTemplate.update("insert into czt_concrete_struct_ratio_main(ccsr_id,condition,create_user,create_time,del_flag,condition_value,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCcsrId(), it.getCondition(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getConditionValue(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public List<CStructureCiEntity> getCStructureCi() {
        String sql = "select * from czt_concrete_struct_ratio_main where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CStructureCiEntity.class));
    }

    @Override
    public CStructureCiEntity getCstructureCiById(String ciId) {
        String sql = "select * from czt_concrete_struct_ratio_main  where ccsr_id ='"+ciId+"'" ;
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(CStructureCiEntity.class));
    }

    @Override
    public void updateCStructureCi(CStructureCiEntity it) {
        String sql = "update czt_concrete_struct_ratio_main set condition= :condition,update_user= :updateUser,update_time= :updateTime,condition_value= :conditionValue,project_id= :projectId" +
                " where ccsr_id= '" + it.getCcsrId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteCStructureCiById(String cStructureCiId) {
        String sql = "update czt_concrete_struct_ratio_main set del_flag = '1' where ccsr_id=?";
        jdbcTemplate.update(sql, cStructureCiId);
    }

    @Override
    public void saverMsCoefficient(MsCoefficientEntity it) {
        jdbcTemplate.update("insert into czt_concrete_pressure_ratio(ccpr_id,ccpr_type,fv_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCcprId(), it.getCcprType(), it.getFvValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }


    @Override
    public List<MsCoefficientEntity> getMsCoefficient() {
        String sql = "select * from czt_concrete_pressure_ratio where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MsCoefficientEntity.class));
    }

    @Override
    public MsCoefficientEntity getMsCoefficientById(String msId) {
        MsCoefficientEntity msCoefficientEntity = null;
        String sql = "select * from czt_concrete_pressure_ratio where ccpr_id = '"+msId+"'";
        List<MsCoefficientEntity> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<MsCoefficientEntity>(MsCoefficientEntity.class));
        if(null!=list&&list.size()>0){
            msCoefficientEntity = list.get(0);
        }
        return msCoefficientEntity;
    }

    @Override
    public void updateMsCoefficient(MsCoefficientEntity it) {
        String sql = "update czt_concrete_pressure_ratio set ccpr_type= :ccprType,update_user= :updateUser,update_time= :updateTime,fv_value= :fvValue,project_id= :projectId" +
                " where ccpr_id= '" + it.getCcprId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteMsCoefficientById(String msCoefficientId) {
        String sql = "update czt_concrete_pressure_ratio set del_flag = '1' where ccpr_id=?";
        jdbcTemplate.update(sql, msCoefficientId);
    }

    @Override
    public void saveCsValue(CsValueEntity it) {
        jdbcTemplate.update("insert into czt_concrete_strength_value(ccsv_id,ccsv_type,strength_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCcsvId(), it.getCcsvType(), it.getStrengthValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public CsValueEntity getCsValueById(String csId) {
        String sql = "select * from czt_concrete_strength_value where ccsv_id = '"+csId+"'" ;
        CsValueEntity csValueEntity = null;
        List<CsValueEntity> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<CsValueEntity>(CsValueEntity.class));
        if(null!=list&&list.size()>0){
            csValueEntity = list.get(0);
        }
        return csValueEntity;
    }


    @Override
    public List<CsValueEntity> getAllCsValue(String titleName) {
        String sql = "select * from czt_concrete_strength_value where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CsValueEntity.class));
    }

    @Override
    public void updateCsValue(CsValueEntity it) {
        String sql = "update czt_concrete_strength_value set ccsv_type= :ccsvType,strength_value= :strengthValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where ccsv_id= '" + it.getCcsvId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteCsValueById(String csValueId) {
        String sql = "update czt_concrete_strength_value set del_flag = '1' where ccsv_id=?";
        jdbcTemplate.update(sql, csValueId);
    }

    @Override
    public Double findValueByTitleNameAndColumn(String titleName, String columnName,String ccsvType ) {
        String sql = "select strength_value from czt_concrete_strength_value where del_flag = '0' and  ccsv_column = (\n" +
                "\n" +
                "select cbrs_column from czt_brick_resistance_sub where cbrs_key = '"+titleName+"'  and info = '"+columnName+"' and cbrs_column is not null ) and ccsv_type= '"+ccsvType+"' ";

        Double value = jdbcTemplate.queryForObject(sql.toString(), Double.class);

        return value;
    }

    @Override
    public Object findEntityByTitleNameAndColumn(String titleName, String columnName,String ccsvType ) {
        Object object = new Object();
        StringBuffer sql = new StringBuffer("select * from ");
        switch (titleName) {
            case "砌体非抗震设计的抗剪强度设计值":
                sql.append("czt_concrete_strength_value where del_flag = '0' and  project_id = (\n" +
                        " select cbrs_id from czt_brick_resistance_sub where cbrs_key = '" + titleName + "'  and info = '" + columnName + "'  ) and ccsv_type= '" + ccsvType + "'");
                try {
                    object = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(CsValueEntity.class));
                    return object;
                }catch (EmptyResultDataAccessException e){
                    return null;
                }
            case "砌体强度的正压力影响系数":
                sql.append("czt_concrete_pressure_ratio where del_flag = '0' and  project_id = (\n" +
                        " select cbrs_id from czt_brick_resistance_sub where cbrs_key = '" + titleName + "'  and info = '" + columnName + "' ) and ccpr_type= '" + ccsvType + "'");
                try {
                    object = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(MsCoefficientEntity.class));
                    return object;
                }catch (EmptyResultDataAccessException e){
                    return null;
                }

        }
        return  object;
    }

    @Override
    public  MsCoefficientEntity findValueByColumn(String ccsvType, double column) {
//        String sql = "select * from  czt_concrete_pressure_ratio where del_flag = '0' and ccpr_column = "+column+" and ccpr_type = '"+ccsvType+"' ;\n";
        String  sql = "select * from czt_concrete_pressure_ratio where del_flag = '0' and ccpr_type  =  '"+ccsvType+"' and project_id = (\n" +
                "select cbrs_id from czt_brick_resistance_sub where cbrs_key  = '砌体强度的正压力影响系数' and del_flag=  '0' and info = '"+column+"')";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MsCoefficientEntity.class));
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public void deleteCStructureCiByProjectId(String arrangeId) {
        String sql = "update czt_concrete_struct_ratio_main set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public void deleteMsCoefficientByProjectId(String arrangeId) {
        String sql = "update czt_concrete_pressure_ratio set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public void deleteCsValueByProjectId(String arrangeId) {
        String sql = "update czt_concrete_strength_value set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public CStructureCiEntity getCStructureCiByCondition(String k, String byCode,Integer code) {
        StringBuilder sql = new StringBuilder("SELECT cm.ccsr_id,condition_value FROM czt_concrete_struct_ratio_main cm LEFT JOIN czt_brick_resistance_sub cs ON cs.cbrs_id = cm.project_id WHERE cs.cbrs_key = '钢筋混凝土结构房屋修正系数Ci值' ");
        if(code>=10){
            sql.append("and cm.condition = '"+byCode+"' and cs.info = '满足'");
        }else {
            sql.append("and cm.condition = '"+k+"' and cs.info = '"+byCode+"'");
        }
        sql.append("AND cs.del_flag = '0' AND cm.del_flag = '0'");
        try {
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(CStructureCiEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public double getMasonryByIdAndType(String id, String bwCategory) {
        String sql = "select strength_value from czt_concrete_strength_value where del_flag = '0' and project_id = '"+id+"' and ccsv_type = '"+bwCategory+"'";
        return jdbcTemplate.queryForObject(sql,double.class);
    }

    @Override
    public String getByGrade(String scom) {
        String sql = "select cbrs_id from czt_brick_resistance_sub where cbrs_key = '砌体非抗震设计的抗剪强度设计值' and info = '"+scom+"'";
        return jdbcTemplate.queryForObject(sql,String.class);
    }
}
